//
//  BPPicPreviewViewController.m
//  BabyPhotograph
//
//  Created by cesh on 2017/3/16.
//  Copyright © 2017年 cesh. All rights reserved.
//

#import "BPPicPreviewViewController.h"
#import "BPPreviewViewController.h"
#import <UIImageView+WebCache.h>
@interface BPPicPreviewViewController () <UICollectionViewDelegate, UICollectionViewDataSource, XLPhotoBrowserDatasource>

//保存所有的图片
@property (strong, nonatomic) NSMutableArray *pics;
//保存所有图片的imgV
@property (strong, nonatomic) NSMutableArray *picViews;

@end

@implementation BPPicPreviewViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.alpha = 1.0;
}

- (NSMutableArray *)pics {
    if (_pics == nil) {
        _pics = [NSMutableArray array];
    }
    return _pics;
}

- (NSMutableArray *)picViews {
    if (_picViews == nil) {
        _picViews = [NSMutableArray array];
    }
    return _picViews;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"摄影师作品";
    self.view.backgroundColor = [UIColor colorWithRed:0xec / 255.0 green:0xf5 / 255.0 blue:0xfc / 255.0 alpha:1.0];
    //图片选择的collectionView
    CGFloat collectionPadding = 4;
    // 创建流水布局
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 行间距
    flowLayout.minimumLineSpacing = collectionPadding;
    // 每个item 之间的间距
    flowLayout.minimumInteritemSpacing = collectionPadding;
    
    // 设置每个cell 的尺寸
    CGFloat cellWH = (screenW - 3 * collectionPadding) * 0.25;
    flowLayout.itemSize = CGSizeMake(cellWH, cellWH);
    
    // 设置滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *collectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH - 64) collectionViewLayout:flowLayout];
    collectionV.backgroundColor = [UIColor clearColor];
    [self.view addSubview:collectionV];
    [collectionV registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"choosePicCell"];
    collectionV.delegate = self;
    collectionV.dataSource = self;
}

#pragma mark - collection view datasource
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"choosePicCell" forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[UICollectionViewCell alloc]init];
    }
    
    UIImageView *picView = [[UIImageView alloc]init];
    cell.backgroundView = picView;
    [picView sd_setImageWithURL:[NSURL URLWithString:self.imgs[indexPath.row]] placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
    [self.picViews addObject:picView];
    
    return cell;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgs.count;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

#pragma mark - collection view delegate
//点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:indexPath.row imageCount:self.imgs.count datasource:self];
    [browser hideChooseBtn];
    browser.browserStyle = XLPhotoBrowserStyleSimple;
}

#pragma mark - XLPhotoBrowserDatasource

- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    UIImageView *imgV = self.picViews[index];
    return imgV.image;
}

- (UIView *)photoBrowser:(XLPhotoBrowser *)browser sourceImageViewForIndex:(NSInteger)index
{
    return self.picViews[index];
}


@end
